package com.oracle.application.controller;

import com.oracle.application.config.PayConfig;
import com.oracle.application.entity.Orders;
import com.oracle.application.entity.vo.PayAsyncVo;
import com.oracle.application.entity.vo.PayVo;
import com.oracle.application.entity.vo.R;
import com.oracle.application.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PayController {

    @Autowired
    private IOrdersService iOrdersService;

    @Autowired
    private PayConfig payConfig;
    @GetMapping("/pay")
    public String pay(Integer id){
        Orders orders=this.iOrdersService.getById(id);
        PayVo payVo=new PayVo();
        payVo.setTotal_amount(orders.getTotalprice().toString());
        payVo.setOut_trade_no(orders.getOrdersn());
        payVo.setSubject("手机商城支付测试");
        String page=this.payConfig.doPay(payVo,id);
        return page;
    }

    //支付宝支付后 异步的请求回调地址 用户验签 和业务处理 并返回给支付宝业务的状态 当核心业务有问题时 必须返回给支付宝失败 非核心出现问题不必返回失败
    @PostMapping("/pay/notify_url")
    public String notify_url(PayAsyncVo payAsyncVo){
        if(this.payConfig.verifysignture(payAsyncVo)){
            return "success";
        }
        return "failure";
    }

    //同步回调
    @GetMapping("/pay/return_url")
    public R return_url(PayAsyncVo payAsyncVo){
        if(this.payConfig.returnfySignture(payAsyncVo)){
            return new R(200, "success","支付成功,等待发货");
        }
        return new R(201,"fail","支付发生问题,请核实后再次支付");

    }
}
